package com.app.server.impl;

import com.app.common.Result;
import com.app.mapper.AdminPermissionMapper;
import com.app.model.AdminPermissionModel;
import com.app.server.AdminPermissionService;
import com.app.utils.FindChildrenUitl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

@Service
public class AdminPermissionServiceImpl extends ServiceImpl<AdminPermissionMapper, AdminPermissionModel> implements AdminPermissionService {

    @Autowired
    private AdminPermissionMapper adminPermissionMapper;
    @Override
    public List<AdminPermissionModel> getMenuList(List<AdminPermissionModel> adminPermissionList) {
        List<Integer> arr1 = new ArrayList<>();
        for (int i = 0; i < adminPermissionList.size(); i++) {
            arr1.add(adminPermissionList.get(i).getId());
        }
        Integer ArrId = 1;
        boolean result1 = arr1.contains(ArrId);
        List<Integer> pidArr1 = new ArrayList<>();
        if (!result1) {
            for (AdminPermissionModel adminPermission : adminPermissionList) {
                boolean pidResult = arr1.contains(adminPermission.getPid());
                if (!pidResult) {
                    pidArr1.add(adminPermission.getPid());
                }
            }
            pidArr1.add(ArrId);
            arr1.addAll(pidArr1);
            LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(arr1);
            ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
            List<AdminPermissionModel> AdminPermissionModels1 = new ArrayList<>();
            for (Integer listWithoutDuplicate : listWithoutDuplicates) {
                AdminPermissionModel AdminPermissionModel = adminPermissionMapper.selectById(listWithoutDuplicate);
                AdminPermissionModels1.add(AdminPermissionModel);
            }
            for (int i = 0; i < listWithoutDuplicates.size(); i++) {
                AdminPermissionModel AdminPermissionModel = adminPermissionMapper.selectById(listWithoutDuplicates);
                AdminPermissionModels1.add(AdminPermissionModel);
            }
            List<AdminPermissionModel> children = FindChildrenUitl.findChildren(AdminPermissionModels1,0);
            return children;
        }
        List<AdminPermissionModel> childrenList = FindChildrenUitl.findChildren(adminPermissionList, 0);
        return childrenList;
    }
}
